#include "Tracer.h"
#include <stdio.h>
#include <stdlib.h>
#include "resource.h"
#include <windows.h>

using namespace std;

CTracer::CTracer(void)
{
	initialized = false;
	mdl = new COBJFile("ffff");
	mdl->Load();
}

CTracer::~CTracer(void)
{
	delete mdl;
}

bool CTracer::init()
{
	HBITMAP hBMP;
	BITMAP	BMP;
	unsigned char Texture[] = { IDB_STONE, IDB_WURST };
	GLuint tmpTex = 0;

	glGenTextures(sizeof(Texture), &texture[0]);				// Generate 3 Textures (sizeof(Texture)=3 ID's)
	for (int loop=0; loop<sizeof(Texture); loop++)				// Loop Through All The ID's (Bitmap Images)
	{
		hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(Texture[loop]), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
		if (hBMP)												// Does The Bitmap Exist?
		{														// If So...
			GetObject(hBMP,sizeof(BMP), &BMP);					// Get The Object
																// hBMP: Handle To Graphics Object
																// sizeof(BMP): Size Of Buffer For Object Information
																// Buffer For Object Information
			glPixelStorei(GL_UNPACK_ALIGNMENT,4);				// Pixel Storage Mode (Word Alignment / 4 Bytes)
			glBindTexture(GL_TEXTURE_2D, texture[loop]);		// Bind Our Texture
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Linear Filtering
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); // Mipmap Linear Filtering

			// Generate Mipmapped Texture (3 Bytes, Width, Height And Data From The BMP)
			gluBuild2DMipmaps(GL_TEXTURE_2D, 3, BMP.bmWidth, BMP.bmHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits);
			DeleteObject(hBMP);									// Delete The Bitmap Object
		}
	}

    char *ext = (char*)glGetString( GL_EXTENSIONS );

    if( strstr( ext, "GL_ARB_point_parameters" ) == NULL )
    {
        MessageBox(NULL,"GL_ARB_point_parameters extension was not found", "ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;
    }
    
	createModel();
	return true;
}

void CTracer::createModel()
{
	mdldl = glGenLists(1);
	glNewList(mdldl, GL_COMPILE);
	glColor4f(1,1,1,1);
	glBindTexture(GL_TEXTURE_2D, texture[0]);

	int i, j;
	for (i = 0; i < mdl->GetNumFaces(); ++i)
	{
		glBegin (GL_TRIANGLES);
		for (j = 0; j < mdl->GetNumVertsPerFace(); ++j)
		{
			if (mdl->HasTextureCoords()) glTexCoord3f (mdl->GetFaceTextureVertex(i,j)->u,mdl->GetFaceTextureVertex(i,j)->v,mdl->GetFaceTextureVertex(i,j)->w);//mdl->texCoords[mdl->faces[i].uvw_indices[j]].uvw);
			glNormal3f (mdl->GetFaceNormalVertex(i,j)->i,mdl->GetFaceNormalVertex(i,j)->j,mdl->GetFaceNormalVertex(i,j)->k);//mdl->normals[mdl->faces[i].norm_indices[j]].ijk);
			glVertex3f(mdl->GetFaceVertex(i,j)->x,mdl->GetFaceVertex(i,j)->y,mdl->GetFaceVertex(i,j)->z);
		}
		glEnd();
	}
	glEndList();
}

void CTracer::drawModel()
{
	glCallList(mdldl);
}

void CTracer::draw()
{
	if(!initialized) initialized = init();

	glLoadIdentity();
	glTranslatef(-0.5f,-2.5f,-12.0f);
	glRotatef(0.8f,1,1,1);
	drawModel();
}